package org.cheetah.spring.xml.factory;

import java.util.ArrayList;
import java.util.List;

import org.cheetah.activities.Activity;
import org.cheetah.activities.Switch;
import org.cheetah.activities.SwitchCase;
import org.springframework.beans.factory.config.AbstractFactoryBean;

public class SwitchProcessorFactoryBean extends AbstractFactoryBean<Switch> {

	private String var;

	private List<String> caseValue;

	private List<?> caseThen;

	private List<?> defaultCaseThen;

	public void setVar(String var) {
		this.var = var;
	}

	public void setCaseValue(List<String> caseValue) {
		this.caseValue = caseValue;
	}

	public void setCaseThen(List<?> caseThen) {
		this.caseThen = caseThen;
	}

	public void setDefaultCaseThen(List<?> defaultCaseThen) {
		this.defaultCaseThen = defaultCaseThen;
	}

	public Class<?> getObjectType() {
		return Switch.class;
	}

	protected Switch createInstance() throws Exception {
		ArrayList<SwitchCase> cases = new ArrayList<SwitchCase>();

		SwitchCase _case;
		int count = caseThen.size();

		for (int i = 0; i < count; ++i) {
			_case = new SwitchCase();

			_case.setValue(caseValue.get(i));
			_case.setActivities(((List<?>) caseThen.get(i))
					.toArray(new Activity[0]));

			cases.add(_case);
		}

		Switch instance = new Switch();
		instance.setVar(var);
		instance.setCases(cases.toArray(new SwitchCase[0]));

		if (defaultCaseThen != null) {
			instance.setDefaultActivities(defaultCaseThen
					.toArray(new Activity[0]));
		}

		return instance;
	}
}
